package com.hiv.service;

import java.util.List;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.RandomUtil;
import com.hiv.common.PageInfo;
import com.hiv.common.param.page.PageQueryBarCodeParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hiv.mapper.BarCodeMapper;
import com.hiv.mapper.domain.BarCode;
import org.springframework.transaction.annotation.Transactional;

/**
 * 条码Service业务层处理
 * 
 * @author zhanghong
 * @date 2022-09-20
 */
@Service
public class BarCodeService {
    @Autowired
    private BarCodeMapper barCodeMapper;
    @Autowired
    private HospitalService hospitalService;

    /**
     * 分页查询
     * @param param
     * @return
     */
    public PageInfo<BarCode> pageQuery(PageQueryBarCodeParam param) {
        long count = barCodeMapper.pageCount(param);
        List<BarCode> list = barCodeMapper.pageQuery(param, param.getPageSize(), param.fetchOffSet());
        list.stream().forEach(item -> buildInfo(item));
        return new PageInfo<>(param.getPageNum(), param.getPageSize(), list, count);
    }

    /**
     * 数据组装
     * @param barCode
     */
    private void buildInfo(BarCode barCode) {
        barCode.setHospitalName(hospitalService.getHospitalName(barCode.getHospitalId()));
    }

    /**
     * 查询条码
     * 
     * @param id 条码主键
     * @return 条码
     */
    public BarCode getById(Long id) {
        BarCode barCode = barCodeMapper.selectById(id);
        buildInfo(barCode);
        return barCode;
    }

    /**
     * 查询条码
     *
     * @param code 条码code
     * @return 条码
     */
    public BarCode getByCode(String code) {
        BarCode barCode = barCodeMapper.queryByCode(code);
        buildInfo(barCode);
        return barCode;
    }

    /**
     * 查询条码列表
     * 
     * @param barCode 条码
     * @return 条码
     */
    public List<BarCode> selectList(BarCode barCode) {
        return barCodeMapper.selectList(barCode);
    }

    /**
     * 保存条码
     *
     * @param barCode 条码
     * @return 结果
     */
    public Boolean save(BarCode barCode) {
        if (barCode.getId() != null) {
            barCodeMapper.update(barCode);
            return true;
        }
        barCodeMapper.insert(barCode);
        return true;
    }

    /**
     * 按照数量批量生产条码
     *
     * @param barCode 条码
     * @return 结果
     */
    public Boolean batchSave(BarCode barCode) {
        for (int i = 0; i < barCode.getNum(); i++) {
            barCode.setCode(RandomUtil.randomNumbers(10));
            barCodeMapper.insert(barCode);
        }
        return true;
    }

    /**
     * 批量删除条码
     * 
     * @param ids 需要删除的条码主键
     * @return 结果
     */
    public Boolean deleteByIds(String ids) {
        barCodeMapper.deleteByIds(Convert.toStrArray(ids));
        return true;
    }

    /**
     * 出库
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchUpdateStatus(List<Long> param) {
        for (Long id : param) {
            barCodeMapper.updateStatusById(id, 2);
        }
        return true;
    }
}
